package com.fight.strive.sys.modules.rbac.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fight.strive.sys.modules.exception.StriveException;
import com.fight.strive.sys.modules.rbac.dao.RbacUserDepartmentMapper;
import com.fight.strive.sys.modules.rbac.dto.RbacUserDepartmentDto;
import com.fight.strive.sys.modules.rbac.entity.RbacDepartmentEntity;
import com.fight.strive.sys.modules.rbac.entity.RbacUserDepartmentEntity;
import com.fight.strive.sys.modules.rbac.service.RbacDepartmentService;
import com.fight.strive.sys.modules.rbac.service.RbacUserDepartmentService;
import com.fight.strive.sys.utils.CollectionUtils;
import com.fight.strive.sys.utils.ObjectUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

@Service
@Slf4j
public class RbacUserDepartmentServiceImpl
        extends ServiceImpl<RbacUserDepartmentMapper, RbacUserDepartmentEntity>
        implements RbacUserDepartmentService {

    @Resource
    private RbacDepartmentService rbacDepartmentService;

    @Override
    public void saveUserDepartment(RbacUserDepartmentEntity entity) {
        try {
            this.saveOrUpdate(entity);
        } catch (DuplicateKeyException e) {
            throw new StriveException("该人员已在该部门下");
        }
    }

    @Override
    public List<RbacUserDepartmentEntity> listByDepartmentId(Long departmentId) {
        QueryWrapper<RbacUserDepartmentEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("department_id", departmentId);
        return this.list(queryWrapper);
    }

    @Override
    public List<Long> listUserIdByDepartmentId(Long departmentId) {
        QueryWrapper<RbacUserDepartmentEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("user_id")
                .eq("department_id", departmentId);
        List<RbacUserDepartmentEntity> list = this.list(queryWrapper);
        if (CollectionUtils.isNotEmpty(list)) {
            return list.stream().map(RbacUserDepartmentEntity::getUserId)
                    .collect(Collectors.toList());
        }
        return Collections.emptyList();
    }

    @Override
    public List<RbacUserDepartmentEntity> listByUserId(Long userId) {
        QueryWrapper<RbacUserDepartmentEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", userId);
        return this.list(queryWrapper);
    }

    @Override
    public List<RbacUserDepartmentDto> listDtoByUserId(Long userId) {
        List<RbacUserDepartmentDto> departments = new ArrayList<>();
        List<RbacUserDepartmentEntity> list = this.listByUserId(userId);
        if (CollectionUtils.isNotEmpty(list)) {
            for (RbacUserDepartmentEntity entity : list) {
                RbacDepartmentEntity department = rbacDepartmentService
                        .getDeparmentById(entity.getDepartmentId());
                if (ObjectUtils.notNull(department)) {
                    departments.add(new RbacUserDepartmentDto(entity, department));
                }
            }
        }
        return departments;
    }

    @Override
    public List<Long> listDepartmentIdByUserId(Long userId) {
        QueryWrapper<RbacUserDepartmentEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("department_id")
                .eq("user_id", userId);
        List<RbacUserDepartmentEntity> list = this.list(queryWrapper);
        if (CollectionUtils.isNotEmpty(list)) {
            return list.stream().map(RbacUserDepartmentEntity::getDepartmentId)
                    .collect(Collectors.toList());
        }
        return Collections.emptyList();
    }

    @Override
    public void removeByDepartmentId(Long departmentId) {
        QueryWrapper<RbacUserDepartmentEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("department_id", departmentId);
        this.remove(queryWrapper);
    }
}
